const synth = window.speechSynthesis
var speaking = false

function playOpt(words, rate=1) {
	try {
		switch (words){
			case '请注意服装配饰等符合如下要求，并做好拍照准备':
				var audio = new Audio('/static/video/1.mp3')
				audio.play()
				audio.addEventListener('ended', function() {
					console.log('音乐播放结束');
				})
				break;
			case '拍照即将开始，请正视前方':
				var audio = new Audio('/static/video/2.mp3')
				audio.play()
				audio.addEventListener('ended', function() {
					console.log('音乐播放结束');
				})
				break;
			case '请按捺左手拇指':
				var audio = new Audio('/static/video/3.mp3')
				audio.play()
				audio.addEventListener('ended', function() {
					console.log('音乐播放结束');
				})
				break;
			case '请按捺右手拇指':
				var audio = new Audio('/static/video/4.mp3')
				audio.play()
				audio.addEventListener('ended', function() {
					console.log('音乐播放结束');
				})
				break;
			default:
				synth.cancel()
				const msg = new SpeechSynthesisUtterance()
				msg.rate = rate
				msg.text = words
				msg.onend = function(event) {
					speaking = false
				}
				synth.speak(msg)
				speaking = true
				break;
		}
	} catch(ex){
		console.log('嗓子卡鸡毛了')
		synth.cancel()
		const msg = new SpeechSynthesisUtterance()
		msg.rate = rate
		msg.text = words
		msg.onend = function(event) {
			speaking.value = false
		}
		synth.speak(msg)
		speaking = true
	}
}